package dao;

import bean.UserBean;
import tools.UtilJDBC;

import java.sql.*;
import java.util.ArrayList;

/**
 * @Author 王志康
 * @Date 2021/8/31 9:51
 * @Version 1.0.0
 */
public class UserDao {
    /**
     * 检查此账号或电话是否已被注册
     *
     * @param user 待注册的用户
     * @return 此用户是否未注册
     */
    public static boolean verification(UserBean user) {
        Connection connection = UtilJDBC.connection;
        String sql = "select user_number from user where user_number=? or user_phone=?";
        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, user.getNumber());
            statement.setString(2,user.getPhoneNumber());
            ResultSet resultSet = statement.executeQuery();
            return !resultSet.next();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 用户注册
     *
     * @param user 传入新注册的用户类
     */
    public static void register(UserBean user) {
        Connection connection = UtilJDBC.connection;
        String sql = "insert into user " +
                "(user_number,user_password,user_integral,user_name,user_phone)" +
                "values(?,?,?,?,?)";
        try {

            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, user.getNumber());
            statement.setString(2, user.getPassword());
            statement.setInt(3, user.getIntegral());
            statement.setString(4, user.getUserName());
            statement.setString(5, user.getPhoneNumber());
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    /**
     * 用户登录
     *
     * @param user 待登录用户
     * @return 此登录的用户数据
     */
    public static UserBean login(UserBean user) {
        Connection connection = UtilJDBC.connection;
        String sql = "select user_number,user_password,user_integral,user_name,user_phone " +
                "from user where user_number=? and user_password=?";
        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, user.getNumber());
            statement.setString(2, user.getPassword());
            ResultSet resultSet = statement.executeQuery();
            if (resultSet.next()) {
                return new UserBean(
                        resultSet.getString("user_number"),
                        resultSet.getString("user_password"),
                        resultSet.getInt("user_integral"),
                        resultSet.getString("user_name"),
                        resultSet.getString("user_phone")
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 更新用户密码或积分
     *
     * @param user 更改的用户信息
     */
    public static void updateUser(UserBean user) {
        Connection connection = UtilJDBC.connection;
        String sql = "update user set " +
                "user_password=?,user_integral=?,user_name=?,user_phone=? " +
                "where user_number=?";
        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, user.getPassword());
            statement.setInt(2, user.getIntegral());
            statement.setString(3,user.getUserName());
            statement.setString(4, user.getPhoneNumber());
            statement.setString(5,user.getNumber());
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除用户(管理员专属方法)
     *
     * @param user 待删除的用户信息
     */
    public static void deleteUser(UserBean user) {
        Connection connection = UtilJDBC.connection;
        String sql = "delete from user where user_number=? and user_password=?";
        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, user.getNumber());
            statement.setString(2, user.getPassword());
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取所有用户信息
     *
     * @return 包含所有用户信息的arraylist
     */
    public static ArrayList<UserBean> getUserList() {
        Connection connection = UtilJDBC.connection;
        ArrayList<UserBean> list = new ArrayList<>();
        String sql = "select user_number,user_password,user_integral,user_name,user_phone " +
                "from user";
        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                list.add(new UserBean(
                        resultSet.getString("user_number"),
                        resultSet.getString("user_password"),
                        resultSet.getInt("user_integral"),
                        resultSet.getString("user_name"),
                        resultSet.getString("user_phone")
                ));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
